<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed'); 
class User_Model extends Model 
{
	var $id = "";	
	var $title = "";
	var $smallImage = "";	
	var $smallInfo = "";
	var $content = "";
	var $thedate = "";
	var $pageview = "";
	var $isNewTech = "";
	
    function User_Model()
    {
        // Call the Model constructor
        parent::Model();
    }
	
// SELECT =================================

	function get_all_user($id,$thedate=0,$num, $offset)
	{
		if($id!='all')
		{
			$sql = "SELECT * FROM news where id <>".$id." and thedate <='".$thedate."' order by  thedate desc limit 8 ";
			$query = $this->db->query($sql);
			if ($query->num_rows() <= 0) 
			{
			$sql = "SELECT * FROM news where id <>".$id." and thedate >='".$thedate."' order by  thedate desc limit 8 ";
			$query = $this->db->query($sql);			
			}
		}
		else
		{
			//$sql = "SELECT * FROM news order by thedate desc";
			$this->db->limit($num, $offset);
			$this->db->order_by("thedate", "desc"); 
			$query = $this->db->get('news');
		}
		return $query->result();
	}

	function get_user_detail($id)
	{

		$sql = "SELECT * FROM news where id=".$id;
		$query = $this->db->query($sql);
		
		// update pageview of news
		$sqlpageview = "update news set pageview=pageview+1 where id=".$id;
		$this->db->query($sqlpageview);
		return $query->result();
	}
	
// INSERT =================================

	function insert_new_user($info)
	{
		$this->db->insert('ua_users', $info); 
	}
	
// UPDATE =================================

	function update_user($id)
	{

		$sql = "SELECT * FROM news where id=".$id;
		$query = $this->db->query($sql);
		
		// update pageview of news
		$sqlpageview = "update news set pageview=pageview+1 where id=".$id;
		$this->db->query($sqlpageview);
		return $query->result();
	}
	
// DELETE =================================

	function delete_user($id)
	{

		$sql = "SELECT * FROM news where id=".$id;
		$query = $this->db->query($sql);
		
		// update pageview of news
		$sqlpageview = "update news set pageview=pageview+1 where id=".$id;
		$this->db->query($sqlpageview);
		return $query->result();
	}
	
// FUNCTION MANIPULATION	

	function check_user($name)
	{

		$sql = "SELECT * FROM news where id=".$id;
		$query = $this->db->query($sql);
		
		// update pageview of news
		$sqlpageview = "update news set pageview=pageview+1 where id=".$id;
		$this->db->query($sqlpageview);
		return $query->result();
	}
	
	function check_login($username,$pass,$rememberme='')
	{

		$this->db->like('username', $username);
		$this->db->like('password', $pass); 
		$query = $this->db->get('ua_users');
		return $query;
	}
	function change_password($user_ID,$newpassword)
	{
		$data['password']=md5($newpassword.'dkw');
		$this->db->where('ua_user_id',$user_ID);
		$result=$this->db->update('ua_users',$data);
			if ($result) 
			{
				return 1;
			} 
			else 
			{
				return 0;
			}

	}
}
